import java.sql.*;
import java.util.ResourceBundle;

/**
 * 使用配置文件使用jdbc,修改数据库时，只需改配置文件
 */
public class JDBCTest01 {
    public static void main(String[] args) {

        //资源绑定器
        ResourceBundle bundle = ResourceBundle.getBundle("resources/db");
        //通过属性配置文件拿到信息
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
            conn = DriverManager.getConnection(url,user,password);
            //获取数据库操作对象
            statement = conn.createStatement();
            //执行Sql
            String sql = "select * from people";
            rs = statement.executeQuery(sql);
            //处理查询结果集
            while (rs.next()){

                String name = rs.getString("name");
                String time = rs.getString("birth");
                System.out.println(name+","+time);
                /**结果集和预期结果一致，一致，一致
                 * 张三,15
                 * 李四,16
                 */
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //释放资源，倒着释放 rs-》statement-》conn
            if (rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
